<html>

	<head>
		<meta charset="UTF-8">
		<title>门站每日提取量分析报表</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="stylesheet" href="../../layui/css/layui.css" />
		<link rel="stylesheet" href="../../css/bass.css" />
	</head>

	<body>
		<div class="body">
			<header class="layui-nav" id="headerTop"></header>
			<div class="left_muen" id="left_menu"></div>
			<div class="content_body">

				<!--标题栏-->
				<p class="content_title">数据监控 &nbsp;&nbsp;>&nbsp;&nbsp;<span class="siteType" id="siteType"></span></p>
				<!--标题栏-->

				<!--条件查询-->
				<div class="content_seach">
					<div class="layui-form">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">日期</label>
								<div class="layui-input-inline ml10">
									<input type="text" name="" id="dateStart" placeholder="请选择时间" autocomplete="off" class="layui-input">
								</div>
								<div class="layui-form-mid"></div>
								<div class="layui-input-inline ml10">
									<input type="text" name="" id="dateEnd" placeholder="请选择时间" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-inline">
								<div class="layui-input-inline" style="width:300px;">
									<input type="radio" name="" value="0" title="标方">
									<input type="radio" name="" value="1" title="吉焦">
									<input type="radio" name="" value="1" title="吨">
								</div>
							</div>
							<button class="layui-btn layui-btn-warm search layui-btn-sm" id="search" lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i>查询</button>
						</div>
					</div>
				</div>
				<!--条件查询-->
				<!--表格-->
				<div class="content_table">
					<script type="text/html" id="toolbarDemo">
						<div class="layui-btn-container">
							<!--<a href="" class="layui-btn layui-btn-sm" id="configure"><i class="layui-icon">&#xe620;</i>配置</a>-->
							<a href="" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe663;</i> 设置时间范围</a>
							<!--<a href="" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe608;</i> 保存</a>-->
							<a href="" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe62c;</i> 趋势图</a>
						</div>
					</script>
					<div class="tabTable">
						<table id="table" lay-filter="tableFilter"></table>
						<div class="layui-table-page fr">
							<div id="layui-table-page1"></div>
						</div>
					</div>
				</div>
				<!--表格-->
			</div>

		</div>
		<script src="../../js/common/jquery-1.11.3.min.js"></script>
		<script src="../../layui/layui.js"></script>
		<script src="../../js/common/navList.js"></script>
		<script src="../../js/common/ajax.js"></script>
		<script type="text/html" id="inputStandardConditionSumColor">
			{{# if(d.inputStandardConditionSum > 0.05){ }}
			<span style="color: red;">{{ d.inputStandardConditionSum }}</span> {{# } }}
		</script>
		<script>
			var getHref = window.location.href;
			console.log(getHref);
			var reportIndex, siteName, time;
			reportIndex = getHref.split("?")[1].split("&")[2].split("=")[1];
			siteName = getHref.split("?")[1].split("&")[3].split("=")[1];
			siteName = decodeURI(siteName);
			time = getHref.split("?")[1].split("&")[4].split("=")[1];
			console.log(reportIndex, siteName, time);
			switch(reportIndex) {
				case '10':
					$("#siteType").html("用户提取量分析报表——" + siteName + "站");
					break;
				case '6':
					$("#siteType").html("门站提取量分析报表");
					break;
				case '7':
					$("#siteType").html("LNG提取量分析报表");
					break;
				case '8':
					$("#siteType").html("次高压提取量分析报表");
					break;
				case '9':
					$("#siteType").html("高压提取量分析报表");
					break;
			};
			layui.use(['table', 'laydate', 'form'], function() {
				var table = layui.table,
					form = layui.form,
					laydate = layui.laydate;

				laydate.render({
					elem: '#dateStart',
					type: 'date'
				});
				laydate.render({
					elem: '#dateEnd',
					type: 'time',
					range: true
				});
				var siteNameArr = siteName;
				ajaxGet({
					url: 'monitordata/getListDetailBySiteName',
					data: {
						siteName: siteNameArr,
						startTime: time + '+00:00',
						endTime: time + '+23:59',
						//						startTime: '2019-01-16+18:13',
						//						endTime: '2019-01-18+18:13',
						groupType: 1
					},
					successFull: function(res) {
						console.log(res);
						var inputSite = [];
						var outSite = [];
						var dataCols = [{
							field: 'date',
							title: '时间',
							align: 'center'
						}];
						console.log(res.list);
						if(res.list.length != 0) {
							$.each(res.list, function(i, item) {
								$.each(item.list, function(j, listItem) {
									if(listItem.otherSiteType == 1) { // 1输出量  输出
										outSite.push(listItem.otherSiteName)
									} else if(listItem.otherSiteType == 0) { //0供应量   输入
										inputSite.push(listItem.otherSiteName)

									}
								});
							});
							//							数组去重
							var outSite2 = [];
							var inputSite2 = [];
							if(inputSite.length != 0) {
								$.each(inputSite, function(i, item) {
									if(inputSite2.indexOf(item) == -1) {
										inputSite2.push(item)
									}
								});
								inputSite2.push('供应量合计');
							};
							if(outSite.length != 0) {
								$.each(outSite, function(i, item) {
									if(outSite2.indexOf(item) == -1) {
										outSite2.push(item)
									}
								});
								outSite2.push('输出量合计');
							}
							console.log(inputSite);
							console.log(outSite)
							$.each(inputSite2, function(i, item) {
								if(item == '供应量合计') {
									dataCols.push({
										field: '',
										title: item,
										align: 'center',
										templet: function(d) {
											//											计算供应量
											var insuppNum = 0;
											if(d.list.length != 0) {
												$.each(d.list, function(i, item) {
													if(item.otherSiteType == 0) {
														insuppNum += Number(item.inputStandardConditionSum);
														//														console.log(item.inputStandardConditionSum);
														//														console.log(insuppNum);
													}
												});
												d.insuppNum = insuppNum;
												return '<div>' + insuppNum + '</div>';
											}
										}
									});
								} else {
									dataCols.push({
										field: 'inputStandardConditionSum',
										title: item,
										align: 'center',
										templet: function(d) {
											console.log(d);
											console.log(i, item);
											for(var j = 0; j < d.list.length; j++) {
												if(d.list[j].otherSiteType == 0 || d.list[j].otherSiteType == null || d.list[j].otherSiteType == undefined) {
													if(d.list[j].inputStandardConditionSum == "" || d.list[j].inputStandardConditionSum == null || d.list[j].inputStandardConditionSum == undefined) {
														d.list[j].inputStandardConditionSum = 0;
													};
													return '<div>' + d.list[j].inputStandardConditionSum + '</div>';
												}
											}
										}
									});
								}
							});
							$.each(outSite2, function(i, item) {
								if(item == '输出量合计') {
									dataCols.push({
										field: '',
										title: item,
										align: 'center',
										templet: function(d) {
											//											计算输出量
											var outsuppNum = 0;
											if(d.list.length != 0) {
												$.each(d.list, function(i, item) {
													if(item.otherSiteType == 1) {
														outsuppNum += Number(item.outputStandardConditionSum);
													}
												});
												d.outsuppNum = outsuppNum;
												return '<div>' + outsuppNum + '</div>';
											}
										}
									});
								} else {
									dataCols.push({
										field: 'outputStandardConditionSum',
										title: item,
										align: 'center',
										templet: function(d) {
											for(var j = 0; j < d.list.length; j++) {
												if(d.list[j].otherSiteType == 1 || d.list[j].otherSiteType == null || d.list[j].otherSiteType == undefined) {
													if(d.list[j].outputStandardConditionSum == "" || d.list[j].outputStandardConditionSum == null || d.list[j].outputStandardConditionSum == undefined) {
														d.list[j].outputStandardConditionSum = 0;
													};
													return '<div>' + d.list[j].outputStandardConditionSum + '</div>';
												}
											}
										}
									});

								}
							});
							dataCols.push({
								field: 'wastageRate',
								title: '供输差',
								align: 'center',
								templet: function(d) {
									//										console.log(d);
									//										计算输出量
									var outsuppNum = 0;
									//											计算供应量
									var insuppNum = 0;
									if(d.list.length != 0) {
										$.each(d.list, function(i, item) {
											if(item.otherSiteType == 1) {
												if(item.outputStandardConditionSum == null || item.outputStandardConditionSum == "" || item.outputStandardConditionSum == undefined) {
													item.outputStandardConditionSum = 0;
												}
												console.log('outputStandardConditionSum ===' + item.outputStandardConditionSum)
												outsuppNum += Number(item.outputStandardConditionSum);
											} else if(item.otherSiteType == 0) {
												if(item.inputStandardConditionSum == null || item.inputStandardConditionSum == "" || item.inputStandardConditionSum == undefined) {
													item.inputStandardConditionSum = 0;
												}
												console.log('inputStandardConditionSum === ' + item.inputStandardConditionSum)
												insuppNum += Number(item.inputStandardConditionSum);
											};
										});
										var supply_tran = (insuppNum - outsuppNum) / insuppNum;
										if(!supply_tran) {
											supply_tran = 0;
										};
										supply_tran = supply_tran + '';
										supply_tran = supply_tran.substring(0, supply_tran.indexOf(".") + 6);
										console.log(supply_tran)
										return '<div>' + supply_tran + '%</div>';

									} else {
										return '<div> 暂无数据 </div>';
									}
								}
							});
							console.log(dataCols);
							ajaxTable({
								url: 'monitordata/getListDetailBySiteName',
								idNode: '#table',
								page: true,
								//								urlParme: '&groupType=1&siteName=' + siteNameArr + '&startTime=2019-01-16+18:13&endTime=2019-01-18+18:13',
								urlParme: '&groupType=1&siteName=' + siteNameArr + '&startTime=' + time + '+00:00&endTime=' + time + '+23:59',
								dataCols: dataCols,
								successFull: function(res, curr, count) {
									// console.log(res)
								}
							})
						}

					}
				});
				//条件查询
				form.on('submit(search)', function(data) {
					var startTime = $("#dateStart").val();
					var endTime = $("#dateEnd").val();
					if(endTime == "") {
						layer.msg("请选择结束时间", {
							icon: 5
						});
						return;
					};
					console.log(startTime);
					console.log(endTime);
					var urlParme = "&groupType=1";
					if(startTime != null && startTime != "" && startTime != undefined && endTime != null && endTime != "" && endTime != undefined) {
						var houers = endTime.split("-");
						endTime = startTime + "+" + $.trim(houers[1]);
						startTime = startTime + "+" + $.trim(houers[0]);
						urlParme += "&startTime=" + startTime + "&endTime=" + endTime;
					}

					ajaxGet({
						//						url:'monitordata/getListAll',
						url: 'monitordata/getListDetailBySiteName',
						data: {
							groupType: 1, //1-按天 2-按周 3-按月 4-按年
							siteNames: siteNameArr,
							startTime: startTime,
							endTime: endTime,
						},
						successFull: function(res) {
							console.log(res);
							var inputSite = [];
							var outSite = [];
							var dataCols = [{
								field: 'date',
								title: '时间',
								align: 'center'
							}];
							console.log(res.list);
							if(res.list.length != 0) {
								$.each(res.list, function(i, item) {
									$.each(item.list, function(j, listItem) {
										if(listItem.otherSiteType == 1) { // 1输出量  输出
											outSite.push(listItem.otherSiteName)
										} else if(listItem.otherSiteType == 0) { //0供应量   输入
											inputSite.push(listItem.otherSiteName)

										}
									});
								});
								//							数组去重
								var outSite2 = [];
								var inputSite2 = [];
								if(inputSite.length != 0) {
									$.each(inputSite, function(i, item) {
										if(inputSite2.indexOf(item) == -1) {
											inputSite2.push(item)
										}
									});
									inputSite2.push('供应量合计');
								};
								if(outSite.length != 0) {
									$.each(outSite, function(i, item) {
										if(outSite2.indexOf(item) == -1) {
											outSite2.push(item)
										}
									});
									outSite2.push('输出量合计');
								}
								console.log(inputSite);
								console.log(outSite)
								$.each(inputSite2, function(i, item) {
									if(item == '供应量合计') {
										dataCols.push({
											field: '',
											title: item,
											align: 'center',
											templet: function(d) {
												//											计算供应量
												var insuppNum = 0;
												if(d.list.length != 0) {
													$.each(d.list, function(i, item) {
														if(item.otherSiteType == 0) {
															insuppNum += Number(item.inputStandardConditionSum);
															//														console.log(item.inputStandardConditionSum);
															//														console.log(insuppNum);
														}
													});
													d.insuppNum = insuppNum;
													return '<div>' + insuppNum + '</div>';
												}
											}
										});
									} else {
										dataCols.push({
											field: 'inputStandardConditionSum',
											title: item,
											align: 'center',
											templet: function(d) {
												console.log(d);
												console.log(i, item);
												for(var j = 0; j < d.list.length; j++) {
													if(d.list[j].otherSiteType == 0 || d.list[j].otherSiteType == null || d.list[j].otherSiteType == undefined) {
														if(d.list[j].inputStandardConditionSum == "" || d.list[j].inputStandardConditionSum == null || d.list[j].inputStandardConditionSum == undefined) {
															d.list[j].inputStandardConditionSum = 0;
														};
														return '<div>' + d.list[j].inputStandardConditionSum + '</div>';
													}
												}
											}
										});
									}
								});
								$.each(outSite2, function(i, item) {
									if(item == '输出量合计') {
										dataCols.push({
											field: '',
											title: item,
											align: 'center',
											templet: function(d) {
												//											计算输出量
												var outsuppNum = 0;
												if(d.list.length != 0) {
													$.each(d.list, function(i, item) {
														if(item.otherSiteType == 1) {
															outsuppNum += Number(item.outputStandardConditionSum);
														}
													});
													d.outsuppNum = outsuppNum;
													return '<div>' + outsuppNum + '</div>';
												}
											}
										});
									} else {
										dataCols.push({
											field: 'outputStandardConditionSum',
											title: item,
											align: 'center',
											templet: function(d) {
												for(var j = 0; j < d.list.length; j++) {
													if(d.list[j].otherSiteType == 1 || d.list[j].otherSiteType == null || d.list[j].otherSiteType == undefined) {
														if(d.list[j].outputStandardConditionSum == "" || d.list[j].outputStandardConditionSum == null || d.list[j].outputStandardConditionSum == undefined) {
															d.list[j].outputStandardConditionSum = 0;
														};
														return '<div>' + d.list[j].outputStandardConditionSum + '</div>';
													}
												}
											}
										});

									}
								});
								dataCols.push({
									field: 'wastageRate',
									title: '供输差',
									align: 'center',
									templet: function(d) {
										//										console.log(d);
										//										计算输出量
										var outsuppNum = 0;
										//											计算供应量
										var insuppNum = 0;
										if(d.list.length != 0) {
											$.each(d.list, function(i, item) {
												if(item.otherSiteType == 1) {
													if(item.outputStandardConditionSum == null || item.outputStandardConditionSum == "" || item.outputStandardConditionSum == undefined) {
														item.outputStandardConditionSum = 0;
													}
													console.log('outputStandardConditionSum ===' + item.outputStandardConditionSum)
													outsuppNum += Number(item.outputStandardConditionSum);
												} else if(item.otherSiteType == 0) {
													if(item.inputStandardConditionSum == null || item.inputStandardConditionSum == "" || item.inputStandardConditionSum == undefined) {
														item.inputStandardConditionSum = 0;
													}
													console.log('inputStandardConditionSum === ' + item.inputStandardConditionSum)
													insuppNum += Number(item.inputStandardConditionSum);
												};
											});
											var supply_tran = (insuppNum - outsuppNum) / insuppNum;
											if(!supply_tran) {
												supply_tran = 0;
											};
											supply_tran = supply_tran + '';
											supply_tran = supply_tran.substring(0, supply_tran.indexOf(".") + 6);
											console.log(supply_tran)
											return '<div>' + supply_tran + '%</div>';

										} else {
											return '<div> 暂无数据 </div>';
										}
									}
								});
								console.log(dataCols);
								ajaxTable({
									url: 'monitordata/getListDetailBySiteName',
									idNode: '#table',
									page: true,
									//								urlParme: '&groupType=1&siteName=' + siteNameArr + '&startTime=2019-01-16+18:13&endTime=2019-01-18+18:13',
									urlParme: '&groupType=1&siteName=' + siteNameArr + '&startTime=' + startTime + '&endTime=' + endTime,
									dataCols: dataCols,
									successFull: function(res, curr, count) {
										// console.log(res)
									}
								})
							}
						}
					})
				});

				//监听工具条
				table.on('tool(tableFilter)', function(obj) {
					var data = obj.data;
					if(obj.event === 'detail') {
						layer.msg('ID：' + data.id + ' 的查看操作');
					} else if(obj.event === 'del') {
						layer.confirm('确定删除吗？', function(index) {
							console.log(data);
							$.ajax({
								url: baseUrl + "/user/del",
								type: "POST",
								data: {
									"id": data.id
								},
								dataType: "json",
								success: function(data) {
									if(data.code == 200) {
										obj.del();
										layer.close(index);
										layer.msg("删除成功", {
											icon: 6
										});
									} else {
										layer.msg("删除失败", {
											icon: 5
										});
									}
								}
							});
						});
					} else if(obj.event === 'edit') {
						layer.confirm('确定修改吗？', function(index) {
							location.href = "./adminEdit.html?pos=3&childPos=0&id=" + data.id;
						});
					}
				});

				/* 配置 */
				$("#configure").click(function() {
					/* 再弹出添加界面 */
					layer.open({
						type: 2,
						title: "配置",
						skin: "myclass",
						area: ["720", '470'],
						content: 'configure.html?reportIndex=1',
					});
					return false;
				});

				function inputData(data, field, Symbol) {
					if(!Symbol) {
						Symbol = "";
					};
					if(data.length != 0) {
						for(var i = 0; i < data.length; i++) {
							if(field == 1) {
								if(data[i].weather != '' || data[i].weather != null || data[i].weather != undefined) {
									return '<div>' + data[i].weather + '' + Symbol + '</div>';
								} else {
									return '<div>-</div>';
								}
							};
							if(field == 2) {
								if(data[i].wastageRate != '' || data[i].wastageRate != null || data[i].wastageRate != undefined) {
									return '<div>' + data[i].wastageRate + '' + Symbol + '</div>';
								} else {
									return '<div>-</div>';
								}
							}
							if(field == 3) {
								if(data[i].inputStandardConditionSum != '' || data[i].inputStandardConditionSum != null || data[i].wastageRate != inputStandardConditionSum) {
									return '<div>' + data[i].inputStandardConditionSum + '' + Symbol + '</div>';
								} else {
									return '<div>-</div>';
								}
							}
							if(field == 4) {
								if(data[i].outputStandardConditionSum != '' || data[i].outputStandardConditionSum != null || data[i].outputStandardConditionSum != undefined) {
									return '<div>' + data[i].outputStandardConditionSum + '' + Symbol + '</div>';
								} else {
									return '<div>-</div>';
								}
							}

						}
					} else {
						return '<div>-</div>';
					}
				}
			});
		</script>
	</body>

</html>